package com.lenovo.gpsautotest;

import android.location.GpsStatus;
import android.util.Log;

import com.lenovo.gpsautotest.LocationManager.EventBusEvents.FirstFixEvent;
import com.lenovo.gpsautotest.LocationManager.EventBusEvents.SatelliteStatusEvent;
import com.lenovo.gpsautotest.LocationManager.GpsService;

import org.greenrobot.eventbus.EventBus;

/**
 * @ProjectName: GpsAutoTest
 * @Package: com.lenovo.gpsautotest
 * @ClassName: GpsStatusListener
 * @Description: GpsStatusListener类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/5/30 15:08
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/5/30 15:08
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class GpsStatusListener implements GpsStatus.Listener {
    private static final String TAG = GpsStatusListener.class.getSimpleName();
    private GpsService service;

    public GpsStatusListener(GpsService service) {
        this.service=service;
    }

    @Override
    public void onGpsStatusChanged(int event) {
        GpsStatus gpsStatus = service.locationManager.getGpsStatus(null);
        SatelliteStatusEvent satelliteStatusEvent = null;
        switch (event) {
            //第一次定位
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                Log.i(TAG, "第一次定位");
                FirstFixEvent firstFixEvent = new FirstFixEvent();
                firstFixEvent.setTtff(gpsStatus.getTimeToFirstFix());
                firstFixEvent.setSessionNo(service.mCurrentSessionNo);
                EventBus.getDefault().post(firstFixEvent);
                //
                satelliteStatusEvent = new SatelliteStatusEvent();
                //获取卫星颗数的默认最大值
                satelliteStatusEvent.setMaxSatellites(gpsStatus.getMaxSatellites());
                //创建一个迭代器保存所有卫星
                satelliteStatusEvent.setGpsSatellites(gpsStatus.getSatellites());
                EventBus.getDefault().post(satelliteStatusEvent);
                break;
            //卫星状态改变
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                Log.i(TAG, "卫星状态改变");
                satelliteStatusEvent = new SatelliteStatusEvent();
                //获取卫星颗数的默认最大值
                satelliteStatusEvent.setMaxSatellites(gpsStatus.getMaxSatellites());
                //创建一个迭代器保存所有卫星
                satelliteStatusEvent.setGpsSatellites(gpsStatus.getSatellites());
                EventBus.getDefault().post(satelliteStatusEvent);
                break;
            //定位启动
            case GpsStatus.GPS_EVENT_STARTED:
                Log.i(TAG, "定位启动");
                break;
            //定位结束
            case GpsStatus.GPS_EVENT_STOPPED:
                Log.i(TAG, "定位结束");
                break;
        }
    }
}
